package com.ying.rabbitmqspringbootconsumer.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * ClassName: ACKListener
 * Package: com.ying.rabbitmqspringbootconsumer.listener
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/7/23 16:59
 * @Version 1.0
 */


@Component
public class DLXListener {

    @RabbitListener(queues = "normalQueue")
    public void ackListener(Message message, Channel channel){

        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            int a = 1 / 0;//异常

            System.out.println(new String(message.getBody()));

            System.out.println("业务处理完毕");

            //3.手动确认:告诉RabbitMQ已经消费成功
            /**
             * deliveryTag:消息的唯一标识,从1开始计数
             * multiple:批量确认消息
             */

            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
//            throw new RuntimeException(e);
            System.out.println("发生异常");


            //4.拒绝确认消息:告诉RabbitMQ消费失败
            /**
             * deliveryTag:消息的唯一标识,从1开始计数
             * multiple:批量拒绝确认消息
             * requeue:false,死信
             */

            try {
                channel.basicNack(deliveryTag,true,false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

        }

    }



}
